from django import http
from django.shortcuts import render
# Create your views here.

from django.views import View

from users.forms import UserInfoForm


class MyCoureView(View):
    def get(self, request):
        return render(request, 'usercenter-mycourse.html')

    def post(self, request):
        pass


class MyFav(View):
    def get(self, request):
        return render(request, 'usercenter-fav-org.html')


class MyFavTeac(View):
    def get(self, request):
        return render(request, 'usercenter-fav-teacher.html')


class MyFavCourse(View):
    def get(self, request):
        return render(request, 'usercenter-fav-course.html')


class MyMessage(View):
    def get(self, request):
        return render(request, 'usercenter-message.html')


class UserInfoView(View):
    """
    用户信息的修改
    """

    def post(self, request):
        # 获取数据
        data = request.POST
        # 创建表单对象传入data
        userinfo = UserInfoForm(data=data, instance=request.user)
        # 对data进行验证
        result = userinfo.is_valid()
        # 如出现异常返回
        if not result:
            return http.JsonResponse({
                'status': 'failure',
                'msg': '保存失败！'
            })
        # 未出现异常
        userinfo.save()
        return render(request, 'usercenter-info.html')
